package com.ming.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: tangC
 * @Date: 2020/7/9 16:33
 * @Description:
 */
@Configuration
public class RedisConfig {

    @Value("${jf.redis.hostName}")
    private String host;
    @Value("${jf.redis.7480}")
    private Integer port;

    @Value("{jf.redis.cluster}")
    private boolean cluster;

    @Bean
    public RedisStandaloneConfiguration connection() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
        return redisStandaloneConfiguration;
    }
    @Bean
    @Primary
    public RedisConnectionFactory redisConnectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration) {
        if (cluster){
            Map<String, Object> source = new HashMap<String, Object>();
            source.put("spring.redis.cluster.nodes","60.205.188.230:6379");
            source.put("spring.redis.cluster.timeout", 120);
            source.put("spring.redis.cluster.max-redirects",20);
            RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
            return new LettuceConnectionFactory(redisClusterConfiguration);
        }else {
            GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
            poolConfig.setMaxIdle(20);
            poolConfig.setMaxTotal(20);
            poolConfig.setMinIdle(20);
            Duration connectTimeout = Duration.ofSeconds(120);
            LettucePoolingClientConfiguration lettucePoolingClientConfiguration = LettucePoolingClientConfiguration.builder()
                    .commandTimeout(connectTimeout)
                    .poolConfig(poolConfig)
                    .shutdownTimeout(Duration.ZERO)
                    .build();
            LettuceConnectionFactory lettuceConnectionFactory =
                    new LettuceConnectionFactory(redisStandaloneConfiguration, lettucePoolingClientConfiguration);
            return lettuceConnectionFactory;
        }
    }

    @Bean
    @Primary
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        return container;
    }

    @Bean
    @Primary
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        template.afterPropertiesSet();
        return template;
    }
}
